/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.appointment;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;
import com.unis.model.appointment.Appointment;
import com.unis.service.appointment.AppointmentService;

/**
 * @author Administrator (email:357375084@qq.com)
 * @since 2013-02-27
 */
@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    private AppointmentService appointmentService;

    /****************************************
     * Insert a instace of Appointment
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertAppointment(@RequestBody Appointment appointment) throws Exception {
        appointment.setStatmentId("Appointment.insert");
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        appointment.setUserid(user.getId());
        String status = "failure";
        appointmentService.insert(appointment);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Appointment
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Appointment appointment) throws Exception {
        appointment.setStatmentId("Appointment.update");
        String status = "failure";
        appointmentService.update(appointment);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Appointment
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Appointment appointment) throws Exception {
        appointment.setStatmentId("Appointment.delete");
        String status = "failure";
        appointmentService.batchDelete(appointment);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Appointment
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Appointment appointment = new Appointment();
        //暂时用用户名
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        appointment.setUserid(user.getUserId());
        appointment.setStatmentId("Appointment.queryAsList");
        BeanUtils.populate(appointment, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(appointment, pageMap);
        List<BaseModel> appointmentList = appointmentService.queryAsList(appointment);
        Map<String, Object> renderMap = GridUIMapper.render(appointment, appointmentList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Appointment
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Appointment appointment) throws Exception {
        appointment.setStatmentId("Appointment.queryAsObject");
        return appointmentService.queryAsObject(appointment);
    }

    @RequestMapping("/page")
    public String forword() {
        return "appointment/appointmentList";
    }

    @Resource
    public void setAppointmentService(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }
}
